package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public void reload() {
        //通过上下文域查询bean，获取所有controller；
        //遍历所有controller获取方法；
        //遍历每个方法获取自定义注解判断自定义注解是否存在；
        Map<String, Object> map = applicationContext.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();
        for (Object controller : controllers) {
            //如果不是cglib对象，不往下执行;
            if (!AopUtils.isCglibProxy(controller)) {
                continue;
            }

            //因为Controller上如果贴了shiro注解，shiro就会对该controllier进行cglib（继承）代理
            //这时候从容器中获取到的就是代理对象，代理类上无法获取父类的注解，所以需要先拿父类再从父类中获取注解
            Class clazz = controller.getClass().getSuperclass();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if (annotation!=null){
                    String exprission = annotation.value()[0];
                    List<String> expressions= permissionMapper.selectExpressions();
                    if (!expressions.contains(exprission)) {
                        String value = annotation.value()[1];
                        Permission permission = new Permission();
                        permission.setName(value);
                        permission.setExpression(exprission);
                        //存进数据库
                        permissionMapper.insert(permission);}
                }
            }
        }
    }

    @Override
    public List<String> selectExpressionByEmpId(Long id) {
        List<String> expressions = permissionMapper.selectExpressionByEmpId(id);
        return expressions;
    }
}
